#include "tdlgwave.h"
#include "ui_tdlgwave.h"
#include "tlr.h"
#include <QVBoxLayout>

TDlgWave::TDlgWave(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::TDlgWave)
{
    ui->setupUi(this);

    ui->cbYAxisMode->addItem("手动操作");
    ui->cbYAxisMode->addItem("自动跟随");
    ui->cbYAxisMode->addItem("自动翻页");

    m_customPlot = new QCustomPlot(this);
    m_customPlot->xAxis->setRange(0,100);
    m_customPlot->yAxis->setRange(0,100);
    m_customPlot->xAxis->ticker()->setTickCount(10);
    m_customPlot->yAxis->ticker()->setTickCount(10);
    m_customPlot->legend->setVisible(true);

    setAttribute(Qt::WA_DeleteOnClose);
//    m_customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom);
//    m_customPlot->addGraph();
//    m_customPlot->graph(0)->setPen(QPen(Qt::blue));
//    m_customPlot->graph(0)->setName("电压");


#if 0

    // add two new graphs and set their look:
    m_customPlot->addGraph();
    m_customPlot->graph(0)->setPen(QPen(Qt::blue)); // line color blue for first graph
    m_customPlot->graph(0)->setBrush(QBrush(QColor(0, 0, 255, 20))); // first graph will be filled with translucent blue
    m_customPlot->addGraph();
    m_customPlot->graph(1)->setPen(QPen(Qt::red)); // line color red for second graph
    // generate some points of data (y0 for first, y1 for second graph):
    QVector<double> x(251), y0(251), y1(251);
    for (int i=0; i<251; ++i)
    {
      x[i] = i;
      y0[i] = qExp(-i/150.0)*qCos(i/10.0); // exponentially decaying cosine
      y1[i] = qExp(-i/150.0);              // exponential envelope
    }
    // configure right and top axis to show ticks but no labels:
    // (see QCPAxisRect::setupFullAxesBox for a quicker method to do this)
    m_customPlot->xAxis2->setVisible(false);
    m_customPlot->xAxis2->setTickLabels(false);
    m_customPlot->yAxis2->setVisible(false);
    m_customPlot->yAxis2->setTickLabels(false);
    // pass data points to graphs:
    m_customPlot->graph(0)->setData(x, y0);
    m_customPlot->graph(1)->setData(x, y1);
    // let the ranges scale themselves so graph 0 fits perfectly in the visible area:
    m_customPlot->graph(0)->rescaleAxes();
    // same thing for graph 1, but only enlarge ranges (in case graph 1 is smaller than graph 0):
    m_customPlot->graph(1)->rescaleAxes(true);
    // Note: we could have also just called customPlot->rescaleAxes(); instead
    // Allow user to drag axis ranges with mouse, zoom with mouse wheel and select graphs by clicking:
    m_customPlot->setInteractions(QCP::iRangeDrag | QCP::iRangeZoom | QCP::iSelectPlottables);
    m_customPlot->graph(0)->setName("曲线1");
    m_customPlot->graph(1)->setName("曲线2");
    m_customPlot->legend->setVisible(true);

    //设置单元格数
    connect(m_customPlot->xAxis,QOverload<const QCPRange &,const QCPRange &>::of(&QCPAxis::rangeChanged),this,&TDlgWave::slotRangeChanged);
#endif


    QVBoxLayout *vblContent = new QVBoxLayout;
    vblContent->addWidget(m_customPlot,1);
    vblContent->addWidget(ui->wgtToolBar);
    setLayout(vblContent);

    setWindowTitle("数据示波器");

    //信号槽_随机测试
//    QTimer *tmTest = new QTimer(this);
//    connect(tmTest,&QTimer::timeout,this,&TDlgWave::slotAutoTest);
//    tmTest->start(50);

}

TDlgWave::~TDlgWave()
{
    emit TLR::get()->sigDlgWaveDestroy();
    delete ui;
}

void TDlgWave::slotRangeChanged(const QCPRange &newRange,const QCPRange &oldRange)
{
    if(newRange.size() < 100) {
        m_customPlot->xAxis->setRange(oldRange);
    }
    //  qDebug() << "new" << newRange.bounded( << newRange.maxRange << newRange.size();
}

void TDlgWave::slotAutoTest()
{
    m_customPlot->graph(0)->addData(index,sin(k) * 50);
    index += 1;
    m_customPlot->replot(QCustomPlot::rpQueuedReplot);
    if(ui->chkAutoScale->isChecked()) {
        m_customPlot->yAxis->rescale();
    }
    k += delta;
 //   qDebug() << m_customPlot->xAxis->range().size();
//    if(ui->chkAutoFollow->isChecked()) {
////        if(index >= 80) {
////            m_customPlot->xAxis->setRange(index - 80, index + 20);
////        }
//        if(index >= m_customPlot->xAxis->range().upper) {
//            m_customPlot->xAxis->setRange(m_customPlot->xAxis->range().upper, m_customPlot->xAxis->range().upper + 100);
//        }
//    }

    int mode = ui->cbYAxisMode->currentIndex();
    int upper,lower;
    upper = m_customPlot->xAxis->range().upper;
    lower = m_customPlot->xAxis->range().lower;
    switch (mode) {
    case 0:
        break;
    case 1:
        if(index >= (lower + 80)) {
            m_customPlot->xAxis->setRange(index - 80, index + 20);
        }
        break;
    case 2:
        if(index >= upper) {
            m_customPlot->xAxis->setRange(upper, m_customPlot->xAxis->range().upper + 100);
        }
        break;
    }
}
